<?php

namespace app\api\service\large;

use app\api\service\Basics;
use app\common\library\MysicLog;
use think\Db;


class PaySuccess extends Basics
{
    // 订单模型
    public $model;


    protected $mysicLog;
    /**
     * 构造函数
     * PaySuccess constructor.
     * @param $orderNo
     * @throws \think\exception\DbException
     */
    public function __construct($orderNo)
    {
        // 实例化订单模型
        $this->model = Db::name('large_order')->where(['order_no'=>$orderNo,'pay_status'=>10])->find();
        $this->wxappId = $this->model['wxapp_id'];
        $this->mysicLog = MysicLog::instance();
    }



    /**
     * 获取订单详情
     * @return OrderModel|null
     */
    public function getOrderInfo()
    {
        return $this->model;
    }

    /**
     * 订单支付成功业务处理
     * @param int $payType 支付类型
     * @param array $payData 支付回调数据
     * @return bool
     */
    public function onPaySuccess($payType, $payData)
    {

        $this->mysicLog->setFilename('notify/large');
        $this->mysicLog->info($payType, '支付类型');
        $this->mysicLog->info(json_encode($payData), '支付数据');
        $this->mysicLog->info($this->model, '订单信息');

        $this->mysicLog->info('开始', '回调数据处理');

        try {
            //更新订单状态
            $this->updatePayStatus($payData);
            //更新商户相关数据
            $this->updateWxapp();
            return true;
        } catch(\Exception $e) {
            $this->mysicLog->error(exceptionLogFormat($e), '更新订单状态,更新商户相关数据异常');
            return false;
        }

    }



    /**
     * 更新付款状态
     * @param array $payData 支付回调数据
     * @return bool
     */
    public function updatePayStatus($payData)
    {
        $this->mysicLog->info('开始', '更新订单状态以及付款时间');

        $status = Db::name('large_order')
            ->where(['order_no'=>$this->model['order_no']])
            ->update([
                'pay_time'=>time(),
                'pay_status'=>20,
                'transaction_id' => $payData['transaction_id'],
            ]);
        $this->mysicLog->info($status, '更新订单状态以及付款时间结果');
    }



    /**
     * 更新商户相关数据
     * @return bool
     */
    public function updateWxapp()
    {
        $this->mysicLog->info('开始', '更新商户相关数据');

        //商户额度的金额变更
        $this->mysicLog->info('开始', '商户额度的金额变更');
        Db::name('wxapp')->where(['wxapp_id'=>$this->model['wxapp_id']])->setInc('money',$this->model['recharge_money']);
        $this->mysicLog->info("成功", '商户额度的金额变更');


        //商户大额流水表
        $money_log = [
            'money' => $this->model['recharge_money'],
            'wxapp_id' => $this->model['wxapp_id'],
            'type'=>1,
            'admin_id' => $this->model['admin_id'],
            'order_id' => $this->model['order_id'],
            'create_time' => time(),
        ];
        $this->mysicLog->info($money_log, '商户大额流水表');
        if($this->model['recharge_money']>0){
            $money_log_status = Db::name('large_log')->insertGetId($money_log);
            $this->mysicLog->info($money_log_status, '商户大额流水表数据结果');
        }else{
            $this->mysicLog->info("需要添加的余额小于0", '商户大额流水表数据结果');
        }


    }



}